package com.ahui.ydd.controller;


import com.ahui.ydd.entity.YddTag;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ahui.ydd.common.ResultData;
import com.ahui.ydd.service.YddTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_tag")
@Slf4j
public class YddTagController {

    @Autowired
    private YddTagService yddTagService;


    @GetMapping("/rowInfo")
    public ResultData getById(Long id){
        YddTag yddTag = yddTagService.getById(id);
        if (yddTag != null) {
            return ResultData.ok("",yddTag);
        }
        return ResultData.error("未找到该标签数据");
    }


    @PostMapping("/add")
    public ResultData save(@RequestBody YddTag yddTag){
        log.info(yddTag.toString());
        yddTagService.save(yddTag);
        return ResultData.ok("","");
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody YddTag yddTag){
        log.info(yddTag.toString());
        yddTagService.updateById(yddTag);
        return ResultData.ok("","");
    }

    @GetMapping
    public ResultData allYddTag(){
        List<YddTag> list = yddTagService.list();
        log.info(list.toString());
        return ResultData.ok("",list);
    }

    @GetMapping("/list")
    public ResultData page(String tagename, Integer page , Integer limit){
        log.info("page={},pageSize={},name={}",page,limit,tagename);

        //构造分页构造器
        Page pageInfo=new Page(page,limit);
        //构造条件构造器
        LambdaQueryWrapper<YddTag> queryWrapper =new LambdaQueryWrapper<>();
        //添加条件过滤
        queryWrapper.like(YddTag::getTagname,tagename);

        //执行查询
        yddTagService.page(pageInfo,queryWrapper);
        return ResultData.ok("",pageInfo);
    }

    @GetMapping("/del")
    public ResultData delete(Long id){
        log.info(id.toString());
        yddTagService.removeById(id);
        return ResultData.ok("","");
    }

}
